package com.hoperun.product.net;

import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.hoperun.product.common.Setting;

import android.util.Log;

/**
 * 调用 webservice �?
 *
 * @author wang_donghai
 */
public class WebServiceAccessUtils {

    private static String TAG = "WebServiceAccessUtils";


/**测试外网地址**/
   //public static String url = "http://27349s11n6.qicp.vip/szerp/PublicWebServiceInterface";

 //public static String url = "http://10.20.40.17:8063/szerp/PublicWebServiceInterface";


    /**
     * 现场环境1
     **/

   // private static String url = "http://10.20.40.158:8080/szerpyn/PublicWebServiceInterface";

    /**现场环境2**/
    //private static String url = "http://10.10.151.52:8080/szerp/PublicWebServiceInterface";
    /**
     * 真实环境  越南    http://10.10.115.138:8080/szerpynhj/PublicWebServiceInterface
     **/
  private static String url = "http://10.10.115.138:8080/szerpynhj/PublicWebServiceInterface";

    private static int timeout;

    // private static String nameSpace = "http://tempuri.org/";
    private static String nameSpace = "http://service.commons.hoperun.com/";



    /**
     * 调用服务器的方法
     *
     * @param methodName
     * @param param
     * @return
     */
    public static String call(final String methodName, String param) {
        // url = Setting.getUrl();
        Log.d(TAG, "url" + url);
        timeout = Setting.getSocketTimeout();
        SoapObject request = new SoapObject(nameSpace, methodName);
        if (param != null) {
            request.addProperty("arg0", param);

            Log.d(TAG, "OnWebSercieRequest **** param=" + param);
        }

        final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);

        final HttpTransportSE ht = new HttpTransportSE(url, timeout);
        ht.debug = true;

        FutureTask<String> future = new FutureTask<String>(
                new Callable<String>() {

                    @Override
                    public String call() {
                        String OPT_FLAG = "0";
                        String MSG_INFO = "";
                        try {
                            ht.call(nameSpace + methodName, envelope);
                            if (envelope.getResponse() != null) {
                                OPT_FLAG = "0";
                                MSG_INFO = envelope.getResponse().toString();

                                Log.d(TAG, "OnWebSercieRequest **** MSG_INFO="
                                        + MSG_INFO);
                            }
                        } catch (IOException e) {
                            OPT_FLAG = "1";
                            MSG_INFO = e.toString();
                        } catch (XmlPullParserException e) {
                            OPT_FLAG = "1";
                            MSG_INFO = e.toString();
                        } catch (Exception e) {
                            OPT_FLAG = "1";
                            MSG_INFO = e.toString();
                        }

                        String result = "";
                        JSONObject retJson = new JSONObject();
                        try {
                            retJson.put(Setting.OPT_FLAG, OPT_FLAG);
                            retJson.put(Setting.MSG_INFO, MSG_INFO);

                            result = retJson.toString();
                        } catch (Exception e) {
                            // TODO: handle exception
                            result = "{\"OPT_FLAG\":1,\"MSG_INFO\":\""
                                    + e.toString() + "\"}";
                        }
                        if (OPT_FLAG.equals("1")) {
                            Log.d(TAG, "OnWebSercieRequest **** MSG_INFO="
                                    + MSG_INFO);
                        }
                        return result;
                    }
                });
        new Thread(future).start();
        try {
            return future.get();
        } catch (InterruptedException e) {
            return "{\"OPT_FLAG\":1,\"MSG_INFO\":\"" + e.toString() + "\"}";
        } catch (ExecutionException e) {
            return "{\"OPT_FLAG\":1,\"MSG_INFO\":\"" + e.toString() + "\"}";
        }
    }
}
